<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>JAVA集合工具类详解 | Hui</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="集合的作用?
是数据的容器，可以存放不同类型的对象，集合有很多实现类,作用是一样的，只不过存储的数据结构不一样，在增、删、改查操作的效率上不一样。集合的组成?

接口Colection List Set Map
实现类ArrayList Vector LinkedListHashSet TreeSet HashMap Hashtable
算法Collections1、sort:对一个集合排序2、">
<meta property="og:type" content="article">
<meta property="og:title" content="JAVA集合工具类详解">
<meta property="og:url" content="http://mhuihui.oschina.io/m-pages/2017/01/11/JAVA集合工具类详解/index.html">
<meta property="og:site_name" content="Hui">
<meta property="og:description" content="集合的作用?
是数据的容器，可以存放不同类型的对象，集合有很多实现类,作用是一样的，只不过存储的数据结构不一样，在增、删、改查操作的效率上不一样。集合的组成?

接口Colection List Set Map
实现类ArrayList Vector LinkedListHashSet TreeSet HashMap Hashtable
算法Collections1、sort:对一个集合排序2、">
<meta property="og:updated_time" content="2017-02-10T03:36:00.207Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JAVA集合工具类详解">
<meta name="twitter:description" content="集合的作用?
是数据的容器，可以存放不同类型的对象，集合有很多实现类,作用是一样的，只不过存储的数据结构不一样，在增、删、改查操作的效率上不一样。集合的组成?

接口Colection List Set Map
实现类ArrayList Vector LinkedListHashSet TreeSet HashMap Hashtable
算法Collections1、sort:对一个集合排序2、">
  
    <link rel="alternate" href="/atom.xml" title="Hui" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/m-pages/css/style.css">
  

</head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/m-pages/" id="logo">Hui</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/m-pages/">Home</a>
        
          <a class="main-nav-link" href="/m-pages/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://mhuihui.oschina.io/m-pages"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-JAVA集合工具类详解" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/m-pages/2017/01/11/JAVA集合工具类详解/" class="article-date">
  <time datetime="2017-01-11T04:13:38.000Z" itemprop="datePublished">2017-01-11</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      JAVA集合工具类详解
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <hr>
<h2 id="集合的作用"><a href="#集合的作用" class="headerlink" title="集合的作用?"></a>集合的作用?</h2><blockquote>
<p>是数据的容器，可以存放不同类型的对象，集合有很多实现类,<br>作用是一样的，只不过存储的数据结构不一样，在增、删、改<br>查操作的效率上不一样。<br>集合的组成?</p>
<ul>
<li>接口<br>Colection List Set Map</li>
<li>实现类<br>ArrayList Vector LinkedList<br>HashSet TreeSet HashMap Hashtable</li>
<li>算法<br>Collections<br>1、sort:对一个集合排序<br>2、reverse:返序一个集合<br>3、max:取集合中最大值<br>4、min:</li>
<li>集合的框架结构?<br>Collection(可以直接加入一个对象，不用键名)<br>List(值可以重复) Set(值不可重复)<br>ArrayList Vector LinkedList HashSet TreeSet(可以排序，所以类型必须相同)<br>Map(键名+键值,键名不可以重复，键值可以重复)<br>HashMap Hashtable</li>
<li>集合的通用方法?<br>iterator<br>size<br>add<br>addAll:一个集合中加入另一个集合<br>remove:删除一个元素<br>removeAll:一个集合与中一个集合中的元素比较,若相同将当前集合删除<br>empty():是否为空<br>get(下标):取<br>indexOf(对象):查<br>List遍历的三种方式:</li>
<li>iterator</li>
<li>enumeration</li>
<li>Enumeration e=Collection.enumeration(集合);</li>
</ul>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">while(e.hasMoreElement())</div><div class="line">&#123;</div><div class="line">e.nextElement();</div><div class="line">&#125;</div><div class="line">for</div></pre></td></tr></table></figure>
<p>Set只有三种遍历方式:<br>iterator:迭代器<br>enumeration:序列<br>Hashmap或Hasttabel的相关方法<br>–得到所有键名<br>Set keys=hm.keySet();<br>–得到所有键值<br>Collection col=hm.values();<br>–得到所有键名+键值<br>Set set=hm.entrySet();</p>
<ul>
<li>三、队列:先进先出，从尾部进，从头出</li>
<li><p>四、栈:先进后出,从头部进，从头部出</p>
</li>
<li><p>二、队列线性表的实现</p>
<ul>
<li>1、先进先出，通过尾部进，通过头部出</li>
</ul>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div></pre></td><td class="code"><pre><div class="line">public class ListQuery</div><div class="line">&#123;</div><div class="line">private int maxSize;</div><div class="line">private int size;</div><div class="line">private int a[];</div><div class="line">private int vear=-1;</div><div class="line">private int front=0;</div><div class="line">public ListQuery(int maxSize)</div><div class="line">&#123;</div><div class="line">this.maxSize=maxSize;</div><div class="line">a=new int[maxSize];</div><div class="line">this.size=0;</div><div class="line">&#125;</div><div class="line">public void add(int i)</div><div class="line">&#123;</div><div class="line">if(this.isFull)</div><div class="line">&#123;</div><div class="line">System.out.println(&quot;队已满&quot;);</div><div class="line">return;</div><div class="line">&#125;</div><div class="line">this.a[++vear]=i;</div><div class="line">if(vear==maxSize-1)</div><div class="line">&#123;</div><div class="line">vear=-1;</div><div class="line">&#125;</div><div class="line">this.size++;</div><div class="line">&#125;</div><div class="line">public int remove()</div><div class="line">&#123;</div><div class="line">if(this.isEmpty)</div><div class="line">&#123;</div><div class="line">System.out.println(&quot;队已空&quot;);</div><div class="line">return;</div><div class="line">&#125;</div><div class="line">int temp=a[front++];</div><div class="line">if(front==maxSize)</div><div class="line">&#123;</div><div class="line">front=0;</div><div class="line">&#125;</div><div class="line">&#125;</div><div class="line">public boolean isEmpty()</div><div class="line">&#123;</div><div class="line">return size==0;</div><div class="line">&#125;</div><div class="line">public boolean isFull()</div><div class="line">&#123;</div><div class="line">return this.size==maxSize;</div><div class="line">&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>三、队列链表的实现<br>public class Node<br>{<br>public int data;<br>public Node next;<br>public Node(int data)<br>{<br>this.data=data;<br>}<br>}<br>public class LinkQuery<br>{<br>private Node front=null;<br>private Node vear=null;<br>public void add(int i)<br>{<br>Node newNode=new Node(i);<br>if(front==null &amp;&amp; vear=null)<br>{<br>front=newNode;<br>vear=newNode;<br>}<br>else<br>{<br>vear.next=newNode;<br>vear=newNode;<br>}<br>}<br>public void remove()<br>{<br>if(front==null)<br>{<br>System.out.println(“栈已空”);<br>return;<br>}<br>Node temp=front;<br>front=front.next();<br>if(front==null)<br>{<br>vear=null;<br>}<br>}<br>public boolean isEmpty()<br>{<br>return front==null;<br>}<br>}<br>四、栈,先进后出,只有一个出口，操作在头部进行<br>五、线性表<br>1、排序(从小到大)<br>1、选择法:第一个数与其余的所有数比较，将最小的数放到第一位，<br>第一位不再参与比较。<br>第二个数与其余的所有数比较，将次小的数放到第二位，<br>第二位不再参与比较。<br>for(int i=0;i<a.length()-1;i++) {="" int="" k="i;" for(int="" j="i+1;j<a.length();j++)" if(a[k]="">a[j])<br>{<br>k=j;<br>}<br>}<br>if(k!=i)<br>{<br>int temp=a[k];<br>a[k]=a[i];<br>a[i]=temp<br>}<br>}<br>2、冒泡法排序:<br>相邻的两个数进行比较，将最大的数放到最后面，最后的这个数不再参与比较<br>相邻的两个数进行比较，将次大的数放到次后面，次后的这个数不再参与比较<br>for(int i=0;i<a.length-1;i++) {="" for(int="" j="0;j<a.length-i-1;j++)" if(a[j]="">a[j+1])<br>{<br>swap(j,j+1);<br>}<br>}<br>}<br>3、插入法排序:假设第一数已排好序，如果第二数比第一数小，第一个数右移,第一位放第二个数。<br>int b[]={1000,-100,-99999,10,20,1,100,200,18};<br>for(int i=1;i<a.length;i++) {="" int="" temp="a[i];" sort="i;" while(i="">0 &amp;&amp; temp&lt;a[sort-1])<br>{<br>a[sort]=a[sort-1];<br>sort–;<br>}<br>a[sort]=temp;<br>}</a.length;i++)></a.length-1;i++)></a.length()-1;i++)></p>
<p>一、栈：先进后出，进与出都在头部操作<br>1、线性表的实现<br>public class ListStack<br>{<br>private int maxSize;<br>private int size;<br>private int top;<br>private int a[];<br>public ListStack(int maxSize)<br>{<br>this.maxSize=maxSize;<br>this.size=0;<br>top=-1;<br>a=new int[maxSize];<br>}<br>public void add(int i)<br>{<br>if(this.isFull)<br>{<br>System.out.println(“栈已满”);<br>return;<br>}<br>a[++top]=i;<br>size++;<br>}<br>public int remove()<br>{<br>if(this.isEmpty())<br>{<br>System.out.println(“栈已空”);<br>return -1;<br>}<br>temp=a[top–];<br>size–;<br>return temp;<br>}<br>public boolean isFull()<br>{<br>return this.size==this.maxSize;<br>}<br>public boolean isEmpty()<br>{<br>return this.size==0;<br>}<br>}<br>2、链表的实现<br>public class Node<br>{<br>public int data;<br>public Node next;<br>public Node(int data)<br>{<br>this.data=data;<br>}<br>}<br>public class LinkStack<br>{<br>private Node top;<br>public void push(int i)<br>{<br>Node newNode=new Node(i);<br>if(top==null)<br>{<br>top=newNode;<br>}<br>else<br>{<br>newNode.next=top;<br>top=newNode;<br>}<br>}<br>public int pop()<br>{<br>if(top==null)<br>{<br>System.out.println(“栈已空”);<br>return -1;<br>}<br>int temp=top.data;<br>top=top.nextNode;<br>retutn temp;<br>}<br>public boolean isEmpty()<br>{<br>return top==null;<br>}<br>}<br>二、线性表<br>public class List<br>{<br>int size;//实际元素的个数<br>int maxSize;//最大容量<br>int a[];//承载数据<br>int index;//当前最大的下标值<br>public List(int maxSize)<br>{<br>this.maxSize=maxSize;<br>a=new int[this.maxSize];<br>index=-1;<br>size=0;<br>}<br>public void addLast(int data)<br>{<br>if(this.isFull)<br>{<br>System.out.println(“线性表已满”);<br>return;<br>}<br>this.a[++index]=data;<br>this.size++;<br>}<br>public void print()<br>{<br>for(int i=0;i<size;i++) {="" system.out.println(a[i]);="" }="" *="" 选择法排序:="" 第一个数与剩余的所有数比较，将最小的数放到第一位，第一位不再参与参与比较="" 第二个数与剩余的所有数比较，将次小的数放到第二位，第二位不再参与参与比较="" public="" void="" selectsort()="" for(int="" i="0;i<this.size-1;i++)" int="" k="i;" j="i+1;j<this.size;j++)" if(a[k]="">a[j])<br>{<br>k=j;<br>}<br>}<br>if(k!=i)<br>{<br>int temp=a[k];<br>a[k]=a[i];<br>a[i]=temp;<br>}<br>}<br>}<br>/<em><br>冒泡法:<br>相邻数比较，将最大数放到最后，最后的这个数不再参与比较<br>相邻数比较，将次大数放到最后，次后的这个数不再参与比较
</em>/<br>public void maopaoSort()<br>{<br>for(int i=0;i<this.size-1;i++) {="" boolean="" flag="false;" for(int="" j="0;j<this.size-i-1;j++)" if(a[j]="">a[j+1])<br>{<br>int temp=a[j];<br>a[j]=a[j+1];<br>a[j+1]=temp;<br>flag=true;<br>}<br>}<br>if(flag==flase)<br>{<br>break;<br>}<br>}<br>}<br>/<em><br>插入法:假设第一数已排好序,<br>插入第二数时，第一数与第二个数比较，若第二个数小于第一数,第一数右移,<br>第一个位置放第二个数
</em>/<br>public void insertSort()<br>{<br>for(int i=1;i<this.size;i++) {="" int="" temp="a[i];" sort="i;" while(sort="">0 &amp;&amp; a[sort-1]&gt;temp)<br>{<br>a[sort]=a[sort-1];<br>sort–;<br>}<br>a[sort]=temp;<br>}<br>}<br>public boolean isEmpty()<br>{<br>return this.size==0;<br>}<br>public boolean isFull()<br>{<br>return this.size=this.maxSize;<br>}<br>}</this.size;i++)></this.size-1;i++)></size;i++)></p>
<p>一、线性表<br>public class List<br>{<br>private int maxSize;<br>private int size;<br>private int index;<br>private int a[];<br>public List(int maxSize)<br>{<br>this.maxSize=maxSize;<br>this.size=0;<br>this.index=-1;<br>a=new int[maxSize];<br>}<br>public void addLast(int data)<br>{<br>//如果满直接退出<br>this.a[++index]=data;<br>this.size++;<br>}<br>public void addFirst(int data)<br>{<br>//如果满直接退出<br>for(int j=index;j&gt;=0;j–)<br>{<br>a[j+1]=a[j];<br>}<br>a[0]=data;<br>this.index++;<br>this.size++;<br>}<br>public void addBefore(int oldData,int newData)<br>{<br>int pos=this.find(oldData);<br>if(pos!=-1)<br>{<br>for(int j=index;j&gt;=pos;j–)<br>{<br>a[j+1]=a[j];<br>}<br>a[pos]=newData;<br>this.index++;<br>this.size++;<br>}<br>}<br>public int find(int data)<br>{<br>for(int i=0;i<size;i++) {="" if(data="=a[i])" return="" i;="" }="" -1;="" public="" int="" removelast()="" 如果为空退出="" temp="a[index];" index--;="" size--;="" temp;="" removefirst()="" for(int="" i="0;i<index;i++)" a[i]="a[i+1];" this.index--;="" this.size--;="" remove(int="" deletedata)="" pos="this.find(deleteData);" if(pos="">=0)<br>{<br>for(int i=pos;i<index;i++) {="" a[i]="a[i+1];" }="" size--;="" index--;="" *="" 折半查找="" 1、是在已排好序的数组中处理="" 2、有三个变量="" lower:初始是0="" upper:初始是数据最大的下标="" mid="(lower+upper)/2;" 3、退出条件="" lower="">upper<br>4、逻辑处理<br>if(a[mid]==findData)<br>{<br>return mid;<br>}<br>else if(a[mid]&gt;findData)<br>{<br>upper=mid-1;<br>}<br>else<br>{<br>lower=mid+1;<br>}<br>*/<br>public void halfFind()<br>{<br>int lower=0;<br>int upper=index;<br>while(lower&lt;=upper)<br>{<br>int mid=(lower+upper)/2;<br>if(a[mid]==findData)<br>{<br>return mid;<br>}<br>else if(a[mid&gt;findData])<br>{<br>upper=mid-1;<br>}<br>else<br>{<br>lower=mid+1;<br>}<br>}<br>}<br>public boolean isEmpty()<br>{<br>return this.size==0;<br>}<br>public boolean isFull()<br>{<br>return this.size==this.maxSize;<br>}<br>}</index;i++)></size;i++)></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://mhuihui.oschina.io/m-pages/m-pages/2017/01/11/JAVA集合工具类详解/" data-id="ciyzazar70000s8vg93a8fmtu" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/m-pages/tags/编程/">编程</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/m-pages/2017/01/11/Struts总结/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Struts总结
        
      </div>
    </a>
  
  
    <a href="/m-pages/2017/01/11/Linux安装多个JDK/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Ubuntu将sun jdk替换默认的open jdk</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/m-pages/tags/ide/">ide</a></li><li class="tag-list-item"><a class="tag-list-link" href="/m-pages/tags/linux/">linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/m-pages/tags/编程/">编程</a></li><li class="tag-list-item"><a class="tag-list-link" href="/m-pages/tags/设计/">设计</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/m-pages/tags/ide/" style="font-size: 13.33px;">ide</a> <a href="/m-pages/tags/linux/" style="font-size: 16.67px;">linux</a> <a href="/m-pages/tags/编程/" style="font-size: 20px;">编程</a> <a href="/m-pages/tags/设计/" style="font-size: 10px;">设计</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/m-pages/archives/2017/02/">February 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/m-pages/archives/2017/01/">January 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/m-pages/archives/2016/12/">December 2016</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/m-pages/2017/02/10/go语言学习笔记（四)/">go语言学习笔记（四）</a>
          </li>
        
          <li>
            <a href="/m-pages/2017/02/10/hello-world/">Hello World</a>
          </li>
        
          <li>
            <a href="/m-pages/2017/01/14/go语言学习笔记（三）/">go语言学习笔记（三）</a>
          </li>
        
          <li>
            <a href="/m-pages/2017/01/13/go语言学习笔记（二）/">go语言学习笔记（二）结构体和切片</a>
          </li>
        
          <li>
            <a href="/m-pages/2017/01/12/go语言学习笔记（一）/">go语言开发环境搭建</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2017 hui<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/m-pages/" class="mobile-nav-link">Home</a>
  
    <a href="/m-pages/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/m-pages/fancybox/jquery.fancybox.css">
  <script src="/m-pages/fancybox/jquery.fancybox.pack.js"></script>


<script src="/m-pages/js/script.js"></script>

  </div>
</body>
</html>